﻿using System;
using System.Windows;
using JulMar.Windows.Mvvm;
using MediaBrowser.CustomWindow;
using MediaBrowser.Gui.MvvmUtils.UI;
using MediaBrowser.Gui.ViewModels;
using MediaBrowser.Gui.Views;

namespace MediaBrowser.Gui
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        private SplashScreenMB splashScreenMB;

        public App()
        {
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
        }

        /// <summary>
        /// Démarrage application Initialisation des services ViewMode
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            ViewModel.RegisterKnownServiceTypes();
            ViewModel.ServiceProvider.Add(typeof(ICustomMessageBoxVisualizer), new CustomMessageBoxVisualizer());
            ViewModel.ServiceProvider.Add(typeof(IWindowAboutVisualizer), new WindowAboutVisualizer());
            ViewModel.ServiceProvider.Add(typeof(IOpenFileDialogVisualizer), new OpenFileDialogVisualizer());

            var main = new WindowMain();

            splashScreenMB = new SplashScreenMB();
            ViewModel.ServiceProvider.Add(typeof(ISplashScreenService), new SplashScreenService(splashScreenMB));
            splashScreenMB.Show();

            SplashScreenMBViewModel vm = ((SplashScreenMBViewModel)splashScreenMB.DataContext);

            vm.ReportProgress(20, "Chargement ... ");
            Application.Current.Resources["PageHomeView"] = new PageHomeView();
            vm.ReportProgress(40, "Chargement des Films ...");
            Application.Current.Resources["PageMoviesView"] = new PageMoviesView();
            vm.ReportProgress(60, "Chargement des Images ...");
            Application.Current.Resources["PagePicturesView"] = new PagePicturesView();
            vm.ReportProgress(80, "Chargement des Musiques ...");
            Application.Current.Resources["PageMusicView"] = new PageMusicView();
            vm.ReportProgress(98, "Chargement de la Librairie ...");
            Application.Current.Resources["PageBooksView"] = new PageBooksView();

            splashScreenMB.Close();
            main.Show();
        }

        /// <summary>
        /// Exception non géré 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.Exception.Message + e.Exception.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
                CustomMessageBox.Show(errorMsg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception)
            {
            }
            e.Handled = true;
        }
    }
}
